package com.neusoft.NEPS.controller;

import com.neusoft.NEPS.common.FileUtils;
import com.neusoft.NEPS.entity.User;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.Map;

public class UserRegisterViewController {
    @FXML
    private Button btn_back;
    @FXML
    private TextField txt_phoneNumber;
    @FXML
    private TextField txt_userName;
    @FXML
    private TextField txt_remark;
    @FXML
    private TextField txt_password;
    @FXML TextField txt_confirmedPassword;

    public void back() throws IOException {
        Stage stage = (Stage)btn_back.getScene().getWindow();
        //Scene scene = btn_back.getScene();
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../view/UserLoginView.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

    public void saveUser() throws IOException {
        String phoneNumber = txt_phoneNumber.getText();
        String userName = txt_userName.getText();
        String remark = txt_remark.getText();
        String password = txt_password.getText();
        User newUser = new User(phoneNumber, userName, remark, password);
        Map<String, User> userMap= null;
        FileUtils.writeMapObject("user.txt", userMap);
        userMap.put(newUser.getPhoneNumber(), newUser);
    }
}
